<?php

namespace App\Http\Controllers\Order;

use App\Http\Controllers\BaseController;
use App\Models\Order\Outboundordersub;
use App\Models\Order\Saleordersub;
use App\Models\Order\Salesordersub;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;


class ShowDataController extends BaseController
{
   
    public function showdata(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);

        if ($start_time > 0 && $end_time > $start_time) {
            $today = [
                'start_time' => $start_time,
                'end_time' => $end_time
            ];
            $index1 = [];

            $index1['today'] = Saleordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('total_weight');



            $index2 = [];

            $index2['today'] = Saleordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('total');




            $index3 = [];

            $index3['today'] = Salesordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('weight');





            $index4 = [];

            $index4['today'] = Salesordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('total');




            $index5 = [];

            $index5['today'] = Outboundordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->sum('weight');




            $index6 = [];

            $index6['today'] = Outboundordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->sum('total');



            $index7 = [];
            $index7['today'] = $index1['today'] > 0 ? mathfuncmoney('div', $index2['today'], $index1['today']) : '';

            $returndata = compact('index1', 'index2', 'index3', 'index4', 'index5', 'index6', 'index7');
        } else {
            $today = [
                'start_time' => mktime(0, 0, 0, date('m'), date('d'), date('Y')),
                'end_time' => time()
            ];
            $yesterday = [
                'start_time' => mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')),
                'end_time' => mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1
            ];

            $index1 = [];

            $index1['today'] = Saleordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('total_weight');
            
            $index1['yesterday'] = Saleordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $yesterday['start_time']],
                ['create_at', '<=', $yesterday['end_time']],
            ])->where('is_del', 1)->sum('total_weight');

           



            $index2 = [];

            $index2['today'] = Saleordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('total');

            $index2['yesterday'] = Saleordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $yesterday['start_time']],
                ['create_at', '<=', $yesterday['end_time']],
            ])->where('is_del', 1)->sum('total');




            $index3 = [];

            $index3['today'] = Salesordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('weight');

            $index3['yesterday'] = Salesordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $yesterday['start_time']],
                ['create_at', '<=', $yesterday['end_time']],
            ])->where('is_del', 1)->sum('weight');





            $index4 = [];

            $index4['today'] = Salesordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('total');

            $index4['yesterday'] = Salesordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $yesterday['start_time']],
                ['create_at', '<=', $yesterday['end_time']],
            ])->where('is_del', 1)->sum('total');




            $index5 = [];

            $index5['today'] = Outboundordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->sum('weight');

            $index5['yesterday'] = Outboundordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $yesterday['start_time']],
                ['create_at', '<=', $yesterday['end_time']],
            ])->sum('weight');




            $index6 = [];

            $index6['today'] = Outboundordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->sum('total');

            $index6['yesterday'] = Outboundordersub::where('cpid', $cpid)->where([
                ['create_at', '>=', $yesterday['start_time']],
                ['create_at', '<=', $yesterday['end_time']],
            ])->sum('total');



            $index7 = [];
            $index7['today'] = $index1['today'] > 0 ? mathfuncmoney('div', $index2['today'], $index1['today']) : '';
            $index7['yesterday'] = $index1['yesterday'] > 0 ? mathfuncmoney('div', $index2['yesterday'], $index1['yesterday']) : '';

            $returndata = compact('index1', 'index2', 'index3', 'index4', 'index5', 'index6', 'index7');
        }


        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $returndata]);
    }
}
